package com.office.web;

import com.office.bean.TDepart;
import com.office.service.DepartService;
import com.office.utils.DepartBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DepartController {


    /**
     *1，新增
     */
    @Autowired
    DepartService departService;


    @RequestMapping("/Depart_add")
    private void addDepart(TDepart depart,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("新增的部门是："+depart);
        int i = departService.addDepart(depart);
        if (i>0){
            resp.getWriter().write("<script>alert('新增成功');location.href='/xxxxx'</script>");
        }else {
            resp.getWriter().write("<script>alert('新增失败');location.href='/html_departadd'</script>");
        }
    }
    /**
     * 2.列表分页
     */
    @RequestMapping("/Depart_list")
    public String depart(long pageindex, long pagesize, Model model){
        System.out.println("起始页面为："+pageindex+"页面大小为："+pagesize);
        //创建Hashmap来接受对象
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex",(pageindex-1)*pagesize);//当前页面从第几条数据开始
        map.put("pagesize",pagesize);//页面显示的大小

        long totalcount = departService.findallcount();//查询总记录数；
        List<TDepart> departslist = departService.findpagecount(map);//查询到的记录
        //放入到utils里面
        DepartBeanUtils<TDepart> pageUtils = new DepartBeanUtils<>(pageindex, pagesize, totalcount, departslist);
        model.addAttribute("PageUtils",pageUtils);//Mode相当于以前的requestion
        return "departlist";
    }
    @RequestMapping("/Depart_delect")
    @ResponseBody
    public boolean delect(int id){
        System.out.println("删除的id为："+id);
        boolean delectdepart = departService.delectdepart(id);
        System.out.println(delectdepart);
        return delectdepart;
    }
    @RequestMapping("/Depart_toupdate/{id}")
    private String to_update(@PathVariable("id") int id,Model model){
        System.out.println("要编辑的id为："+id);
        TDepart depart = departService.findDepartByid(id);
        model.addAttribute("depart",depart);
        return "departupdate";
    }
    @RequestMapping("/Depart_update")
    private void update(TDepart tDepart,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("更新的部门名字为："+tDepart);
        int i = departService.updateDepart(tDepart);
        System.out.println("部门更新成功的状态为："+i);
        if (i>0){
            resp.getWriter().write("<script>alert('更新成功，正在跳转');location.href='/Depart_list?pageindex=1&pagesize=5';</script>");
        }else {
            resp.getWriter().write("<script>alert('更新失败') location.href='/Depart_toupdate/"+tDepart.getId()+"';</script>");
        }
    }
}
